package com.ncs.leisure.beans.memory;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.ncs.leisure.beans.ui.vo.Map;

public class MapMirror {
	
	//索引
	
	Integer id;
	
	//账号列表
	
	HashMap<Integer,BeansMember> personList=null;
	
	//镜像
	
	Map map;
	
	//名称
	
	String name;
	
	//最大人数
	
	Integer num;
	
	//使用密码
	
	boolean locked;
	
	//密码
	
	String password;
	
	//拥有者
	
	Integer owner;
	
	//效果列表
	
	LinkedList<Effect> effectList=null;
	
	//创建时间
	
	Date createTime;
	
	//房间状态
	
	String state;
	
	//聊天室
	
	List<String> chat=null;
	
	//是否全体准备
	
	public boolean isAllReady(){
		
		Iterator iterator=personList.keySet().iterator();
		
		BeansMember bm=null;
		
		while(iterator.hasNext()){
			
			bm=personList.get((Integer)iterator.next());
			
			if(!bm.state.equals("ready")){
				
				return false;
				
			}
			
		}
		
		return true;
		
	}
	
	//移动坐标
	
	public String movePersonXY(Integer memberId,String ctrlChar){
		
		BeansMember bm=this.getPersonList().get(memberId);

		//获得自己的显示对象
		
		byte state=bm.getState().getBytes()[0];
		
		//查找并获得正确的对象赋值
		
		Map map=this.getMap();
				
		int loc=map.getMapBody().indexOf(state);
		
		//向上
		
		if(ctrlChar.equals("&")){
			
			bm.setDirection("1");
			
			//最上一层不移动
			
			if(loc/map.getWidth()==0){return "ok";}
			
			moveEffects(memberId,state,loc,loc-map.getWidth());
						
		//向下
			
		}else if(ctrlChar.equals("(")){
			
			bm.setDirection("3");
			
			//最下一层不移动
			
			if(loc/map.getWidth()==map.getHeight()-1){return "ok";}
			
			moveEffects(memberId,state,loc,loc+map.getWidth());
			
		//向左
			
		}else if(ctrlChar.equals("%")){
			
			bm.setDirection("2");
			
			//最左一层不移动
			
			if(loc%map.getWidth()==0){return "ok";}

			moveEffects(memberId,state,loc,loc-1);

		//向右
			
		}else if(ctrlChar.equals("'")){
			
			bm.setDirection("4");
			
			//最右一行不移动
			
			if(loc%map.getWidth()==map.getWidth()-1){return "ok";}

			moveEffects(memberId,state,loc,loc+1);

		}
		
		return "ok";
		
	}
	
	//判断地图变换及道具效果
	
	public void moveEffects(Integer memberId,byte roleChar,int currentLoc,int targetLoc){
		
		byte[] mapByte=map.getMapBody().getBytes();
		
		byte[] mapByteCopy=map.getMapBodyCopy().getBytes();
		
		//mapByte[targetLoc] 将要被替换的标签
		
		
		//透明物体
		
		if(mapByte[targetLoc]=='_'){
			
			mapByte[targetLoc]=roleChar;
			
			mapByte[currentLoc]=mapByteCopy[currentLoc];
		
		//遇到墙	
			
		}else if(mapByte[targetLoc]==' '){
			
		//遇到尸体	
			
		}else if(mapByte[targetLoc]=='+'){
			
			mapByte[targetLoc]=roleChar;
			
			mapByte[currentLoc]=mapByteCopy[currentLoc];
			
		//遇到敌人
			
		}else if(mapByte[targetLoc]>48 && mapByte[targetLoc]<58){
			
			//操作者标记 
			
			String mId=(char)roleChar+"";
			
			String cId=(char)mapByte[targetLoc]+"";
			
			BeansMember mBM=personList.get(memberId);
			
			BeansMember cBM=null;
			
			Iterator iterator =personList.keySet().iterator();
			
			while(iterator.hasNext()){
				
				BeansMember tBM=personList.get(iterator.next());
				
				if(tBM.getState().equals(cId)){
					
					cBM=tBM;
					
				}
				
			}
			
			//没有此玩家
			
			if(cBM!=null){
				
				//给胜利玩家奖励
				
				//标记死亡玩家
				
				cBM.setState("died");
				
				//将地图缓冲变更墓地
				
				mapByteCopy[targetLoc]='+';
				
				map.setMapBodyCopy(new String(mapByteCopy));
				
			}
			
			mapByte[targetLoc]=roleChar;
			
			mapByte[currentLoc]=mapByteCopy[currentLoc];
			
		}
		
		
		map.setMapBody(new String(mapByte));
		
	}
	
	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public MapMirror(){
		
		personList=new HashMap<Integer,BeansMember>();
		
		effectList=new LinkedList<Effect>();
		
	}

	public HashMap<Integer, BeansMember> getPersonList() {
		return personList;
	}

	public boolean isLocked() {
		return locked;
	}

	public void setLocked(boolean locked) {
		this.locked = locked;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPersonList(HashMap<Integer, BeansMember> personList) {
		this.personList = personList;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getNum() {
		return num;
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	public LinkedList<Effect> getEffectList() {
		return effectList;
	}

	public void setEffectList(LinkedList<Effect> effectList) {
		this.effectList = effectList;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getOwner() {
		return owner;
	}

	public void setOwner(Integer owner) {
		this.owner = owner;
	}

	public List<String> getChat() {
		return chat;
	}

	public void setChat(List<String> chat) {
		this.chat = chat;
	}

}
